package com.woniuxy.ticket.ticketmovie.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticket.ticketmovie.entity.Movie;
import com.woniuxy.ticket.ticketmovie.entity.MovieType;
import com.woniuxy.ticket.ticketmovie.mapper.MovieMapper;
import com.woniuxy.ticket.ticketmovie.mapper.MovieTypeMapper;
import com.woniuxy.ticket.ticketmovie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    @Autowired
    private MovieTypeMapper movieTypeMapper;

    //根据类型id分页查询电影
    @Override
    public List<Movie> findMovieByTypeId(Page<Movie> page, int id) {
        return movieMapper.findMovieByTypeId(page, id);
    }

    @Transactional
    @Override
    public void add(Movie movie, int[] typeIds) {
        movieMapper.insert(movie);

        for (int typeId : typeIds) {
            MovieType movieType = new MovieType();
            movieType.setMovieId(movie.getId());
            movieType.setTypeId(typeId);
            movieTypeMapper.insert(movieType);
        }

    }
}
